<template>
  <div class="app-container">
     <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="会员等级名称" prop="name">
           <el-input
              v-model="queryParams.name"
              placeholder="请输入会员等级名称"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
           />
        </el-form-item>
        <!-- <el-form-item label="会员等级名称" prop="postName">
           <el-input
              v-model="queryParams.postName"
              placeholder="请输入会员等级名称"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
           />
        </el-form-item>
        <el-form-item label="状态" prop="status">
           <el-select v-model="queryParams.status" placeholder="会员等级状态" clearable style="width: 200px">
              <el-option
                 v-for="dict in sys_normal_disable"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
              />
           </el-select>
        </el-form-item> -->
        <el-form-item>
           <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
           <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
     </el-form>

     <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
           <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['level:index:add']"
           >新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
           <el-button
              type="success"
              plain
              icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:post:edit']"
           >修改</el-button>
        </el-col>
        <el-col :span="1.5">
           <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:post:remove']"
           >删除</el-button>
        </el-col> -->
        <!-- <el-col :span="1.5">
           <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              v-hasPermi="['goods:index:export']"
           >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar> -->
     </el-row>

     <el-table v-loading="loading" :data="levelList" @selection-change="handleSelectionChange">

        <el-table-column
          label="等级名称"
          align="center"
          prop="name"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="套餐名称"
          align="center"
          prop="packageName"
          :show-overflow-tooltip="true"
          width="150"
        />
        <el-table-column label="类型" align="center" prop="type" width="100">
           <template #default="scope">
              <dict-tag :options="level_type" :value="scope.row.type" />
           </template>
        </el-table-column>

        <el-table-column
          label="购买价格"
          align="center"
          prop="price"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column label="套餐图片" align="center" prop="status" width="100">
           <template #default="scope">
            <ImagePreview ref="ImagePreview" :src="scope.row.picUrl" :width="60" :height="60"></ImagePreview>
            <!-- src/components/ImagePreview -->
           </template>
        </el-table-column>
        <el-table-column
          label="赠送饭券"
          align="center"
          prop="givePoint"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="赠送饭团"
          align="center"
          prop="giveContribution"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="赠送饭点"
          align="center"
          prop="pointContribution"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="赠送礼盒数"
          align="center"
          prop="suitNum"
          :show-overflow-tooltip="true"
          width="110"
        />
        <el-table-column label="备注" align="center" prop="status" width="300">
           <template #default="scope">
             {{ scope.row.remark ||'-' }}
           </template>
        </el-table-column>
        <el-table-column
          label="显示顺序"
          align="center"
          prop="sort"
          :show-overflow-tooltip="true"
          width="80"
        />

        <el-table-column label="创建时间" align="center"  prop="createTime" width="200">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right" align="center" class-name="small-padding fixed-width">
           <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['level:index:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['level:index:remove']">删除</el-button>
           </template>
        </el-table-column>
     </el-table>

     <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
     />

     <!-- 添加或修改会员等级对话框 -->
     <el-dialog :title="title" v-model="open" width="800px" append-to-body>
        <div class="form_box">
          <el-form ref="levelRef" :model="form" :rules="rules" label-width="100px">
            <el-row>
            <el-col :span="12">
              <el-form-item label="等级名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入等级名称" />
              </el-form-item>
            </el-col>




            <el-col :span="12">
              <el-form-item label="购买价格" prop="price">
                <el-input-number v-model="form.price" controls-position="right" :min="0" style="width: 100%;"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="类型" prop="type">
                  <el-radio-group v-model="form.type">
                    <el-radio
                      v-for="dict in level_type"
                      :key="dict.value"
                      :label="dict.value"
                    >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>

            </el-col>
            <el-col :span="12">
              <el-form-item label="套餐名称" prop="packageName">
                <el-input v-model="form.packageName" placeholder="请输入套餐名称" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="套餐图片" prop="picUrl">
                  <ImageUpload ref="ImageUploads" v-model:modelValue="form.picUrl" :limit="1"></ImageUpload>
                </el-form-item>
              </el-col>
            <el-col :span="12">
              <el-form-item label="赠送饭券" prop="givePoint">
                <el-input-number v-model="form.givePoint" controls-position="right" :min="0" style="width: 100%;"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="赠送饭团" prop="giveContribution">
                <el-input-number v-model="form.giveContribution" controls-position="right" :min="0" style="width: 100%;"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="赠送饭点" prop="pointContribution">
                <el-input-number v-model="form.pointContribution" controls-position="right" :min="0" style="width: 100%;"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="赠送礼盒数" prop="suitNum">
                <el-input-number v-model="form.suitNum" controls-position="right" :min="0" style="width: 100%;"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序" prop="sort">
                <el-input-number v-model="form.sort" controls-position="right" :min="0" style="width: 100%;"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        </div>

        <template #footer>
           <div class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
           </div>
        </template>
     </el-dialog>
  </div>
</template>

<script setup name="goodsManage">
import { getMemberLevelList,addMemberLevel,deleteMemberLevel,editMemberLevel,getMemberLevelDetail} from "@/api/member/level";
// import { getlevelList,addGoods,deleteGoods,editGoods,getGoodsDetail} from "@/api/goods/manage";
const { proxy } = getCurrentInstance();
const { goods_status,goods_type } = proxy.useDict("goods_status","goods_type");

const levelList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const level_type = ref([
{
    elTagClass: "",
    elTagType: "success",
    label :"普通会员",
    value: "1"
  },
  {
    elTagClass: "",
    elTagType: "danger",
    label :"广告套餐",
    value: "2"
  }
]);

const data = reactive({
 form: {},
 queryParams: {
   pageNum: 1,
   pageSize: 10,
   name: undefined,
   status: undefined
 },
 rules: {
  name: [
    { required: true, message: "等级名称不能为空", trigger: "blur" }
  ],
  sort: [
    { required: true, message: "排序不能为空", trigger: "blur" }
  ],
  price: [
    { required: true, message: "购买价格不能为空", trigger: "blur" }
  ],
  givePoint: [
    { required: true, message: "赠送积分不能为空", trigger: "blur" }
  ],
  giveContribution: [
    { required: true, message: "贡献值不能为空", trigger: "blur" }
  ],
  pointContribution: [
    { required: true, message: "贡献点不能为空", trigger: "blur" }
  ],
  suitNum: [
    { required: true, message: "赠送礼盒数不能为空", trigger: "blur" }
  ],
  remark: [
    { required: true, message: "备注不能为空", trigger: "blur" }
  ],
 }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询会员等级列表 */
function getList() {
 loading.value = true;
 getMemberLevelList(queryParams.value).then(response => {
   levelList.value = response.rows;
   total.value = response.total;
   loading.value = false;
 });
}
/** 取消按钮 */
function cancel() {
 open.value = false;
 reset();
}
/** 表单重置 */
function reset() {
 form.value = {
    id: undefined,
    name: undefined,
    type:'1',
    price:0,
    givePoint:undefined,
    giveContribution:undefined,
    remark:undefined,
    sort: undefined,
    status: "0",
    pointContribution:undefined,
    suitNum:undefined,
    picUrl:undefined,
    packageName:undefined
 };
 proxy.resetForm("levelRef");
}
/** 搜索按钮操作 */
function handleQuery() {
 queryParams.value.pageNum = 1;
 getList();
}
/** 重置按钮操作 */
function resetQuery() {
 proxy.resetForm("queryRef");
 handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
 ids.value = selection.map(item => item.postId);
 single.value = selection.length != 1;
 multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
 reset();
 open.value = true;
 title.value = "添加会员等级";
}
/** 修改按钮操作 */
function handleUpdate(row) {
 reset();
 const id =  row.id;
 getMemberLevelDetail(id).then(response => {
   response.data.status = response.data.status+'';
   response.data.type = response.data.type+'';
   form.value = response.data;
   title.value = "修改会员等级";
   open.value = true;

 });
}
/** 提交按钮 */
function submitForm() {
 proxy.$refs["levelRef"].validate(valid => {
   if (valid) {
      if(form.value.picUrl===''||form.value.picUrl===undefined||form.value.picUrl===null){
        proxy.$modal.msgWarning("请添加套餐图片");
        return
      }
     if (form.value.id != undefined) {
      editMemberLevel(form.value).then(response => {
         proxy.$modal.msgSuccess("修改成功");
         open.value = false;
         getList();
       });
     } else {
      addMemberLevel(form.value).then(response => {
         proxy.$modal.msgSuccess("新增成功");
         open.value = false;
         getList();
       });
     }
   }
 });
}
/** 删除按钮操作 */
function handleDelete(row) {
 const id =  row.id;
 proxy.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项？').then(function() {
   return deleteMemberLevel(id);
 }).then(() => {
   getList();
   proxy.$modal.msgSuccess("删除成功");
 }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
 proxy.download("system/post/export", {
   ...queryParams.value
 }, `post_${new Date().getTime()}.xlsx`);
}




getList();
</script>
<style lang="scss" scoped>
.form_box {
  // height: 60vh;
  // overflow: auto;
}
</style>
